package com.min.star.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.model.Room;
import com.min.star.model.Grade;
import com.min.star.service.IRoomService;
import com.min.star.service.IGradeService;
import com.min.star.ui.Json;

@Controller
@RequestMapping("/room")
public class RoomController {

	private Log log = LogFactory.getLog(RoomController.class);

	@Autowired
	private IRoomService roomService;

	@RequestMapping("/open")
	public String open(Model model) {
		return "/other/roomList";
	}

	@RequestMapping("/map")
	@ResponseBody // 将方法返回值序列化为json
	public Object map(Room room) {
//	通过page对象设置查询条件,指定分页的页码和页的大小
		Page<Room> page = new Page<Room>(room.getPageNumber(), room.getPageSize());
//	通过page条件查询出来的记录存放在另一个page中
		Page<Room> pager = this.roomService.selectPage(page, room);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", pager.getRecords());
		map.put("total", pager.getTotal());
		System.out.println(map);
		return map;
	}

	@PostMapping("/add")
	@ResponseBody
	public Json add(@Valid Room room, BindingResult result) {
		Json j = new Json();
		if (result.hasErrors()) {
			return errorMessage(result, j);
		}
		boolean exist = this.roomService.checkRoomTimeExist4Add(room);
		if (exist) {
			j.setMsg("课程时间与地点重复，新增失败！");
			j.setCode(-1);
		} else {
			Room info = this.roomService.save(room);
			j.setMsg("新增成功！");
			j.setCode(0);
			j.setObj(info);
		}
		return j;
	}

	private Json errorMessage(BindingResult result, Json j) {
		String msg = "";
		List<ObjectError> allErrors = result.getAllErrors();
		for (int i = 0; i < allErrors.size(); i++) {
			ObjectError er = allErrors.get(i);
			if (i == 0) {
				msg += er.getDefaultMessage() + "<br/>";
			} else {
				msg += "<i class='mdi mdi-emoticon-sad'></i> " + er.getDefaultMessage() + "<br/>";
			}
		}
		j.setMsg(msg);
		j.setCode(-2);
		j.setObj(allErrors);
		return j;
	}

	@PostMapping("/edit")
	@ResponseBody
	public Json edit(@Valid Room room, BindingResult result) {
		Json j = new Json();
		if (result.hasErrors()) {
			return errorMessage(result, j);
		}
		boolean exist = this.roomService.checkRoomTimeExist4Edit(room);
		if (exist) {
			j.setMsg("课程时间与地点重复，更新失败！");
			j.setCode(-1);
		} else {
			Room info = this.roomService.update(room);
			j.setMsg("更新成功！");
			j.setCode(0);
			j.setObj(info);
		}
		return j;
	}

	@GetMapping("/delete/{ids}")
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		Json j = new Json();
		int ret = this.roomService.delete(ids);
		if (ret > 0) {
			j.setMsg("操作成功！");
			j.setCode(0);
		} else {
			j.setMsg("操作失败！");
			j.setCode(-1);
		}
		return j;
	}

}
